import { obj_bool, loc, obj_empty, get_num, obj_str, is_empty } from 'sui';
import { pad_3 } from 'nui';
import { four_lever, top_lever, second_lever, third_lever } from './tree';

const format_ok = (v) => {
    const reg = /[\s'"]/g;
    let ret = v.replaceAll(reg, "");
    return ret;
};

const get_plc_type = (plc, only_int=false, ext=false) => {
    if (only_int) {
        let ret = [];

        if (plc === "s7") {
            ret = [
                {id: "bool", des: loc("cmd_67")},
                {id: "byte", des: loc("cmd_68")},
                {id: "int16", des: loc("cmd_69")},
                {id: "int32", des: loc("cmd_70")},
            ];
        }
        else {
            ret = [
                {id: "bool", des: loc("cmd_67")},
                {id: "byte", des: loc("cmd_68")},
                {id: "int16", des: loc("cmd_69")},
                {id: "int32", des: loc("cmd_70")},
                {id: "int64", des: loc("cmd_71")},
            ];
        }

        if (ext) {
            ret.push({id: "float", des: loc("cmd_72")});
            ret.push({id: "string", des: loc("cmd_73")});
        }

        return ret;
    }

    if (plc === "s7") {
        return [
            {id: "bool", des: loc("cmd_67")},
            {id: "arraybool", des: loc("zh_12")},
            {id: "char", des: loc("zh_13")},
            {id: "byte", des: loc("cmd_68")},
            {id: "int16", des: loc("cmd_69")},
            {id: "int32", des: loc("cmd_70")},
            {id: "float", des: loc("cmd_72")},
            {id: "string", des: loc("cmd_73")},
        ];
    }

    return [
        {id: "bool", des: loc("cmd_67")},
        {id: "byte", des: loc("cmd_68")},
        {id: "int16", des: loc("cmd_69")},
        {id: "int32", des: loc("cmd_70")},
        {id: "int64", des: loc("cmd_71")},
        {id: "float", des: loc("cmd_72")},
        {id: "string", des: loc("cmd_73")},
    ];
};

const plc_label = (pre, d, hcmd, hsub) => {
    const c = get_num(d, "c");
    const tp = obj_str(d, "t");

    let id = obj_str(d, "m");
    if (pre) {
        id = `${pre}.${id}`;
    }

    const ret = {...d};

    ret["index"] = "";
    ret["id"] = id;
    ret["m"] = id;

    if (c < 1) {
        ret["ltp"] = tp;
    }
    else if (tp === "char" && obj_bool(d, "a")) {
        ret["ltp"] = `${tp} [ ${c} ]`;
    }
    else {
        ret["ltp"] = `${tp} [ ${c} ]`;

        const children = [];

        for (let i = 0; i < c; i++) {
            const tid = `${id}.${i}`;
            const tmp = {id: tid, m: tid, ltp: tp};

            children.push(hsub(tmp));
        }

        ret["children"] = children;
    }

    return hcmd(ret);
};

const get_cur_main = (main, hcmd) => {
    if (obj_empty(main)) {
        return hcmd(null);
    }

    const data = main.v;
    if (is_empty(data)) {
        return hcmd(null);
    }

    const ret = []

    data.forEach((d, i) => {
        let tp = obj_str(d, "t");

        if (tp !== "c") {
            const item = plc_label("", d, top_lever, second_lever);
            item["index"] = i + 1;

            ret.push(item);
            return;
        }

        tp = obj_str(d, "cv");
        const v = d.v;
        if (is_empty(v)) {
            return;
        }

        const m = obj_str(d, "m");
        const c = get_num(d, "c");
        const val = {...d};

        const children = [];

        if (c > 0) {
            val["ltp"] = `${tp} [ ${c} ]`;

            for (let i = 0; i < c; i++) {
                const id = `${m}.${i}`;
                const tmp = [];

                v.forEach(t => {
                    const s = plc_label(id, t, third_lever, four_lever);
                    tmp.push(s);
                });

                const item = {id: id, m: id, ltp: tp, children: tmp};
                children.push(second_lever(item));
            }
        }
        else {
            val["ltp"] = tp;

            v.forEach(t => {
                const item = plc_label(m, t, top_lever, second_lever);
                children.push(item);
            });
        }

        val["index"] = i + 1;
        val["id"] = m;
        val["m"] = m;
        val["children"] = children;

        ret.push(top_lever(val));
    });

    hcmd(ret);
};

const get_plc_name = (v) => {
    const tmp = {
        omron: loc("cmd_26"),
        s7: loc("zh_7"),
    };

    return tmp[v];
};

const s7_type = () => {
    return [
        {id: "S200", des: "S200"},
        {id: "S200SMART", des: "S200SMART"},
        {id: "S300", des: "S300"},
        {id: "S400", des: "S400"},
        {id: "S1200", des: "S1200"},
        {id: "S1500", des: "S1500"},
    ];
};

const get_plc = () => {
    return [
        {id: "omron", des: get_plc_name("omron")},
        {id: "s7", des: get_plc_name("s7")},
    ];
};

const it_id = (plc, main) => {
    const set = new Set();
    if (!is_empty(main)) {
        main.forEach(d => set.add(obj_str(d, "uid")));
    }

    for (let i = 1; i <= 1000; i++) {
        const id = `${plc}.it${pad_3(i)}`;
        if (set.has(id)) {
            continue;
        }

        return id;
    }

    return null;
};

const get_uid = (cur, plc, data) => {
    let uid = obj_str(cur, "uid");
    if (!uid) {
        uid = it_id(plc, data);
    }

    return uid;
}

export {s7_type, get_uid, get_plc, get_plc_type, get_plc_name, get_cur_main, format_ok};
